package com.offcn.servlet;

import com.alibaba.fastjson.JSON;
import com.offcn.entity.Course;
import com.offcn.entity.CourseUser;
import com.offcn.entity.User;
import com.offcn.service.CourseService;
import com.offcn.service.CourseUserService;
import com.offcn.service.Impl.CourseServiceImpl;
import com.offcn.service.Impl.CourseUserServiceImpl;
import com.offcn.service.Impl.UserServiceImpl;
import com.offcn.service.UserService;
import com.offcn.utils.PageTool;
import com.offcn.utils.ResultBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.RescaleOp;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@WebServlet("/courseuser")
public class CourseUserServlet extends BaseServlet {
    ResultBean bean = null;

    protected void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String currentPage = request.getParameter("currentPage");
        String pageSize = request.getParameter("pageSize");
        String search = request.getParameter("search");
        System.out.println(search);
        CourseUserService service = new CourseUserServiceImpl();
        UserService userservice = new UserServiceImpl();
        CourseService courseService = new CourseServiceImpl();
        List<User> users = userservice.findByname(search);
        if (users.size() == 0 || users == null) {
            bean = new ResultBean(500, "未查询到对应信息", null);
            response.getWriter().write(JSON.toJSONString(bean));
            return;
        }
        String str = "";
        for (User user : users) {
            str += user.getUid() + ",";
        }
        str = "(" + str.substring(0, str.length() - 1) + ")";
        int totalCount = service.getTotalCount(str);
        PageTool pageTool = new PageTool(totalCount, currentPage, Integer.parseInt(pageSize));
        List<CourseUser> list = service.findByPage(pageTool, str);
        for (CourseUser cu : list) {
            User user = userservice.findByUid(cu.getUid());
            Course course = courseService.findByCid(cu.getCid());
            cu.setCourse(course);
            cu.setCourseName(course.getCourseName());
            cu.setUid(user.getUid());
            cu.setUser(user);
        }
        pageTool.setList(list);
        if (list.size() > 0 && list != null) {
            bean = new ResultBean(200, "查询成功", pageTool);
        } else {
            bean = new ResultBean(500, "查询失败", pageTool);
        }
        response.getWriter().write(JSON.toJSONString(bean));
    }

    protected void findAllCourse(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uid = request.getParameter("uid");
        String cid = request.getParameter("cid");
        CourseService courseService = new CourseServiceImpl();
        List<Course> list = courseService.findCourse(uid, cid);
        if (list.size() > 0) {
            bean = new ResultBean(200, "查询成功", list);
        } else {
            bean = new ResultBean(500, "查询失败", null);
        }
        response.getWriter().write(JSON.toJSONString(bean));
    }

    //    修改选课
    protected void updateCourseUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String cid = request.getParameter("cid");
        String id = request.getParameter("id");
        CourseUserService service = new CourseUserServiceImpl();
        int i = service.updateCourseUser(id, cid);
        if (i > 0) {
            bean = new ResultBean(200, "更新成功", i);
        } else {
            bean = new ResultBean(500, "更新失败", null);
        }
        response.getWriter().write(JSON.toJSONString(bean));
    }

    //    删除选中
    protected void delAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String[] delarrs = request.getParameterValues("delarr");
        String s = Arrays.toString(delarrs);
        String arr = "(" + s.substring(1, s.length() - 1) + ")";
        CourseUserService service = new CourseUserServiceImpl();
        int i = service.delAll(arr);
        if (i > 0) {
            bean = new ResultBean(200, "删除成功", i);
        } else {
            bean = new ResultBean(500, "删除失败", null);
        }
        response.getWriter().write(JSON.toJSONString(bean));
    }
}
